package com.xinweisi.dataserver.module.sysdata.datastatistics;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.OnlineMemberDao;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.dto.OnLineMemberQueryDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.entity.OnLineMemberVO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.dto.MemberDataQueryDTO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.vo.MemberAvgTimeVO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.vo.MemberDataVO;
import com.xinweisi.dataserver.util.DateUtil;
import com.xinweisi.dataserver.util.SmartBeanUtil;
import com.xinweisi.dataserver.util.SmartPageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MemberDataService {

    @Autowired
    private MemberDataDao memberDataDao;

    @Autowired
    private OnlineMemberDao onlineMemberDao;
    /**
     * 获取会员数据
     *
     * @param queryDTO
     * @return
     */
    public ResponseDTO<MemberDataVO> getMemberData(MemberDataQueryDTO queryDTO) {

        List<MemberDataVO> entityList = new ArrayList<>();
        MemberDataVO entity =null;
        for (int i=0;i<queryDTO.getDBNameList().size();i++){
            queryDTO.setDBName(queryDTO.getDBNameList().get(i));
            entity = memberDataDao.selectMemberData(queryDTO);
            entityList.add(entity);
        }
        List<OnLineMemberVO> OnLineMemberList = onlineMemberDao.selectOnLineMemberList(new OnLineMemberQueryDTO());
        entity.setOnLineMemberCount(OnLineMemberList.size()+"");
        MemberDataVO maxAgePerson = entityList.stream().max(Comparator.comparing(MemberDataVO::getOnLineMemberMax)).get();
        entity.setOnLineMemberMax(maxAgePerson.getOnLineMemberMax());
        maxAgePerson = entityList.stream().max(Comparator.comparing(MemberDataVO::getOnLineMemberMin)).get();
        entity.setOnLineMemberMin(maxAgePerson.getOnLineMemberMin());

        return ResponseDTO.succData(entity);
    }

    /**
     * 获取平均时长
     * @author lijie1
     * @date 2020/7/2 0002
     */
    public ResponseDTO<MemberAvgTimeVO> getMemberAvgTime() {
        String DBNameNew = DateUtil.getDate(new Date());
        MemberDataQueryDTO queryDTO = new MemberDataQueryDTO();
        queryDTO.setDBName("daylog_"+DBNameNew);
        MemberAvgTimeVO entity = memberDataDao.getMemberAvgTime(queryDTO);
        return ResponseDTO.succData(entity);
    }
}
